package lib

import (
	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/m"
)

// 买钻石
func BuyDiamond(from string, u *m.User, tid int64, PSend func(interface{}) error) (uProp *m.UpdateProp, resGoods []*m.Goods, boxResList []*m.BoxRes, err error) {
	sd := u.GetShopDiamon()
	defer sd.Update(sd)

	// 获取每日礼包道具
	tpl, ok := data.Get("PaidPropsPacks", uint64(tid))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表PaidPropsPacks")
		return
	}
	tplT := tpl.(data.PaidPropsPack)
	// 增加该付费礼包的次数
	sd.BuyCnt[tid]++
	mkv := make(map[uint64]uint64)

	for _, item := range tplT.Goods {
		mkv[item.Key] = item.Value
	}
	if sd.BuyCnt[tid] == 1 { // 首次购买
		for _, item := range tplT.Firstsend {
			mkv[item.Key] += item.Value
		}
	}
	// 打包道具
	giveGoods := make([]m.BaseKV, 0)
	for key, val := range mkv {
		gtpl, ok := data.Get("Goods", key)
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表Goods")
			return
		}
		gtplT := gtpl.(data.Good)
		if int64(gtplT.Category) == data.GoodsCategoryEnum.CategoryLv3 { // 宝箱，需要一个一个给
			for i := int64(0); i < int64(val); i++ {
				giveGoods = append(giveGoods, m.BaseKV{Key: int64(key), Val: 1})
			}
		} else {
			giveGoods = append(giveGoods, m.BaseKV{Key: int64(key), Val: int64(val)})
		}
	}

	// 给用户道具
	uProp, resGoods, boxResList, err = GiveGoodsByKvList(from, u, giveGoods, PSend)
	if err != nil {
		return
	}
	return
}
